package shuashuami.hb.com.avtivity;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import org.json.JSONException;
import org.json.JSONObject;

import shuashuami.hb.com.hbclient.R;
import shuashuami.hb.com.hdlibrary.util.ToastUtil;
import shuashuami.hb.com.http.HttpMethods;
import shuashuami.hb.com.http.HttpOnNextListener;
import shuashuami.hb.com.share.ShareCUserInfo;

public class CAgencyApplicationActivity extends AbActivity {

    private EditText edtApplicant, edtPhone, edtMessage;
    private Button btnSubmit;

    @Override
    protected void initUI() {
        setAbContentView(R.layout.activity_cagency_application);
        edtApplicant = (EditText) findViewById(R.id.edt_cagenct_application_user);
        edtPhone = (EditText) findViewById(R.id.edt_cagenct_application_phone_num);
        edtMessage = (EditText) findViewById(R.id.edt_cagenct_application_message);
        btnSubmit = (Button) findViewById(R.id.btn_cagency_application_submit);
    }

    @Override
    protected void initData() {
        setTitleBar("代理申请");
        setLeftView();
    }

    @Override
    protected void initListener() {
        btnSubmit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (checkAccount())
                    perfectData();
            }
        });
    }

    public void perfectData() {
        HttpOnNextListener<String> listener = new HttpOnNextListener<String>() {
            @Override
            public void onNext(String s) {
                Message message = handler.obtainMessage(1);     // Message
                message.obj = s;
                handler.sendMessageDelayed(message, 0);

            }
        };
        ShareCUserInfo shareCUserInfo = new ShareCUserInfo(this);
        HttpMethods.agentApplyc(listener,
                shareCUserInfo.getId(), edtApplicant.getText().toString(),
                edtPhone.getText().toString(), edtMessage.getText().toString());
    }

    final Handler handler = new Handler() {

        public void handleMessage(Message msg) {         // handle message
            switch (msg.what) {
                case 1:
                    String result = msg.obj.toString();
                    try {
                        JSONObject jsonObject = new JSONObject(result);
                        if (jsonObject.getInt("status") == 200) {
                            ToastUtil.showShort(CAgencyApplicationActivity.this, jsonObject.getString("message"));
                            setResult(RESULT_CANCELED);
                            finish();
                        } else {
                            ToastUtil.showShort(CAgencyApplicationActivity.this, jsonObject.getString("message"));
                        }
                    } catch (JSONException e) {
                        e.printStackTrace();
                    }
                    break;
            }

            super.handleMessage(msg);
        }
    };

    private boolean checkAccount() {
        if (edtApplicant.getText().toString().trim().isEmpty()) {
            edtApplicant.setError("申请人不能为空");
            edtApplicant.requestFocus();
            return false;
        }
        if (edtPhone.getText().toString().trim().isEmpty()) {
            edtPhone.setError("手机号码不能为空");
            edtPhone.requestFocus();
            return false;
        }
        if (edtMessage.getText().toString().trim().isEmpty()) {
            edtMessage.setError("理由不能为空");
            edtMessage.requestFocus();
            return false;
        }
        return true;
    }

}
